/*
 * DesktopApplication1View.java
 */
package dmcn.gui.input;

import au.com.bytecode.opencsv.CSVReader;
import dmcn.bl.genetics.execute.ExecuteGenetic;
import dmcn.bl.genetics.input.Input;
import dmcn.bl.genetics.input.InputCalculator;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.InputVerifier;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import org.jgap.InvalidConfigurationException;

/**
 * The application's main frame.
 */
public class DmcnApplicationMainUi extends FrameView {

    public DmcnApplicationMainUi(SingleFrameApplication app) {
        super(app);
        application = app;
        initComponents();
        initListeners();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = DmcnApplication.getApplication().getMainFrame();
            aboutBox = new DmcnApplicationAboutUi(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        DmcnApplication.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        xLabel = new javax.swing.JLabel();
        yLabel = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        executeGenetic = new javax.swing.JButton();
        numOfPopulation = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        moreOptionCb = new javax.swing.JCheckBox();
        crossOverPer = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        mutation = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        terminalLoads = new javax.swing.JButton();
        terminalCosts = new javax.swing.JButton();
        concetratorCapacity = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        csvInputCb = new javax.swing.JCheckBox();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(DmcnApplicationMainUi.class);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setToolTipText(resourceMap.getString("jTextField1.toolTipText")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.setPreferredSize(new java.awt.Dimension(50, 20));

        xLabel.setText(resourceMap.getString("xLabel.text")); // NOI18N
        xLabel.setName("xLabel"); // NOI18N

        yLabel.setText(resourceMap.getString("yLabel.text")); // NOI18N
        yLabel.setName("yLabel"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setToolTipText(resourceMap.getString("jTextField2.toolTipText")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.setPreferredSize(new java.awt.Dimension(50, 20));

        jSeparator1.setName("jSeparator1"); // NOI18N

        executeGenetic.setText(resourceMap.getString("executeGenetic.text")); // NOI18N
        executeGenetic.setName("executeGenetic"); // NOI18N

        numOfPopulation.setText(resourceMap.getString("numOfPopulation.text")); // NOI18N
        numOfPopulation.setToolTipText(resourceMap.getString("numOfPopulation.toolTipText")); // NOI18N
        numOfPopulation.setName("numOfPopulation"); // NOI18N
        numOfPopulation.setPreferredSize(new java.awt.Dimension(50, 20));

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        moreOptionCb.setText(resourceMap.getString("moreOptionCb.text")); // NOI18N
        moreOptionCb.setToolTipText(resourceMap.getString("moreOptionCb.toolTipText")); // NOI18N
        moreOptionCb.setName("moreOptionCb"); // NOI18N

        crossOverPer.setText(resourceMap.getString("crossOverPer.text")); // NOI18N
        crossOverPer.setToolTipText(resourceMap.getString("crossOverPer.toolTipText")); // NOI18N
        crossOverPer.setEnabled(false);
        crossOverPer.setName("crossOverPer"); // NOI18N
        crossOverPer.setPreferredSize(new java.awt.Dimension(50, 20));

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        mutation.setText(resourceMap.getString("mutation.text")); // NOI18N
        mutation.setToolTipText(resourceMap.getString("mutation.toolTipText")); // NOI18N
        mutation.setEnabled(false);
        mutation.setName("mutation"); // NOI18N
        mutation.setPreferredSize(new java.awt.Dimension(50, 20));

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        terminalLoads.setText(resourceMap.getString("terminalLoads.text")); // NOI18N
        terminalLoads.setName("terminalLoads"); // NOI18N

        terminalCosts.setText(resourceMap.getString("terminalCosts.text")); // NOI18N
        terminalCosts.setName("terminalCosts"); // NOI18N

        concetratorCapacity.setText(resourceMap.getString("concetratorCapacity.text")); // NOI18N
        concetratorCapacity.setName("concetratorCapacity"); // NOI18N

        jLabel1.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel2.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel7.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel7.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel7.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        csvInputCb.setText(resourceMap.getString("csvInputCb.text")); // NOI18N
        csvInputCb.setToolTipText(resourceMap.getString("csvInputCb.toolTipText")); // NOI18N
        csvInputCb.setName("csvInputCb"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(executeGenetic)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(moreOptionCb)
                                .addGap(18, 18, 18)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(crossOverPer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(numOfPopulation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel5)
                                        .addComponent(jLabel3)))
                                .addGap(55, 55, 55)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(mutation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addGap(8, 8, 8)
                                        .addComponent(jLabel6)))))
                        .addContainerGap(184, Short.MAX_VALUE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(xLabel)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(yLabel)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(terminalLoads)
                            .addComponent(terminalCosts)
                            .addComponent(concetratorCapacity)
                            .addComponent(csvInputCb))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7)
                            .addComponent(jLabel2))
                        .addGap(47, 47, 47))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(csvInputCb)
                .addGap(10, 10, 10)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(xLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(yLabel)
                        .addGap(9, 9, 9)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(terminalLoads)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(terminalCosts)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(concetratorCapacity)
                            .addComponent(jLabel7))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(numOfPopulation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(crossOverPer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(mutation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(moreOptionCb))
                .addGap(18, 18, 18)
                .addComponent(executeGenetic)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(DmcnApplicationMainUi.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 286, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton concetratorCapacity;
    private javax.swing.JTextField crossOverPer;
    private javax.swing.JCheckBox csvInputCb;
    private javax.swing.JButton executeGenetic;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JCheckBox moreOptionCb;
    private javax.swing.JTextField mutation;
    private javax.swing.JTextField numOfPopulation;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JButton terminalCosts;
    private javax.swing.JButton terminalLoads;
    private javax.swing.JLabel xLabel;
    private javax.swing.JLabel yLabel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    private SingleFrameApplication application;
    private boolean moreOptions = false;
    private SwingWorker sw;
    private static final String ADDRESS_FILE = "input.csv";
    private Input input;
    private InputCalculator calculator;
    private int[][] appCost;
    private int[] appFortio;
    private int[] appXwritikothta;
    private String pressedString = "Pressed!";
    private boolean isFromCsv = false;

    private void initListeners() {

        calculator = new InputCalculator();
        jLabel1.setVisible(false);
        jLabel2.setVisible(false);
        jLabel7.setVisible(false);


        moreOptionCb.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    mutation.setEnabled(true);
                    crossOverPer.setEnabled(true);
                    moreOptions = true;
                } else {
                    mutation.setEnabled(false);
                    crossOverPer.setEnabled(false);
                    moreOptions = false;
                }
            }
        });

        csvInputCb.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    jTextField1.setEnabled(false);
                    jTextField2.setEnabled(false);
                    terminalLoads.setEnabled(false);
                    terminalCosts.setEnabled(false);
                    concetratorCapacity.setEnabled(false);
                    isFromCsv = true;
                    initInput();
                    jTextField1.setText(input.getSygkentrotes().toString());
                    jTextField2.setText(input.getPhges().toString());
                } else {
                    mutation.setEnabled(false);
                    jTextField1.setEnabled(true);
                    jTextField2.setEnabled(true);
                    terminalLoads.setEnabled(true);
                    terminalCosts.setEnabled(true);
                    concetratorCapacity.setEnabled(true);
                    isFromCsv = false;
                }
            }
        });

        terminalLoads.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                appFortio = calculator.calculatefortio(Integer.parseInt(jTextField2.getText()));
                jLabel1.setText(pressedString);
                jLabel1.setVisible(true);
            }
        });
        terminalCosts.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                appCost = calculator.calculateCost(Integer.parseInt(jTextField1.getText()), Integer.parseInt(jTextField2.getText()));
                jLabel2.setText(pressedString);
                jLabel2.setVisible(true);
            }
        });
        concetratorCapacity.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                appXwritikothta = calculator.calculateXwritikotita(Integer.parseInt(jTextField1.getText()));
                jLabel7.setText(pressedString);
                jLabel7.setVisible(true);
            }
        });


        executeGenetic.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {

                sw = new SwingWorker() {

                    @Override
                    protected Object doInBackground() throws Exception {
                        try {
                            executeGenetic.setEnabled(false);
                            ExecuteGenetic eg = new ExecuteGenetic();
                            if (isFromCsv) {
//                                initInput();
                                eg.executeGenetic(
                                        input.getSygkentrotes(),//sygkentrwtes
                                        input.getPhges(),//phges
                                        input.getKostos(),//cost
                                        input.getFortio(),//fortio
                                        input.getXwritikothta(),//xwrhtikothta
                                        Integer.parseInt(numOfPopulation.getText()),//population size
                                        0,//num of evolves
                                        true,//more options
                                        Integer.parseInt(mutation.getText()),
                                        Double.parseDouble(crossOverPer.getText()),
                                        1,
                                        10000 //approximation limit
                                        );
                            } else {
                                eg.executeGenetic(
                                        Integer.parseInt(jTextField1.getText()),//sygkentrwtes
                                        Integer.parseInt(jTextField2.getText()),//phges
                                        appCost,//cost
                                        appFortio,//fortio
                                        appXwritikothta,//xwrhtikothta
                                        Integer.parseInt(numOfPopulation.getText()),//population size
                                        0,//num of evolves
                                        true,//more options
                                        Integer.parseInt(mutation.getText()),
                                        Double.parseDouble(crossOverPer.getText()),
                                        1,
                                        10000 //approximation limit
                                        );
                            }

                        } catch (InvalidConfigurationException ex) {
                            Logger.getLogger(DmcnApplicationMainUi.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        return null;
                    }

                    @Override
                    protected void done() {
                        super.done();
                        progressBar.setVisible(false);
                        executeGenetic.setEnabled(true);
                    }
                };
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
                progressBar.setValue(0);

                sw.addPropertyChangeListener(new PropertyChangeListener() {

                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        progressBar.setValue(sw.getProgress());
                    }
                });

                sw.execute();

            }
        });

//        cancelButton.addMouseListener(new MouseAdapter() {
//
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                sw.cancel(true);
//            }
//        });
    }

    private void initInput()  {
        try {
            CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE), ";".charAt(0));
            String[] nextLine = reader.readNext();
            Integer phges = Integer.parseInt(nextLine[0]);
            System.out.println("terminals: " + phges);
            nextLine = reader.readNext();
            Integer sygkentrotes = Integer.parseInt(nextLine[0]);
            System.out.println("concetrators: " + sygkentrotes);
            int[] fortio = new int[phges];
            int[] xwritikothta = new int[sygkentrotes];
            int[][] kostos = new int[sygkentrotes][phges];
            nextLine = reader.readNext();
            for (int i = 0; i < phges; i++) {
                fortio[i] = Integer.parseInt(nextLine[i]);
            }
            nextLine = reader.readNext();
            for (int i = 0; i < sygkentrotes; i++) {
                xwritikothta[i] = Integer.parseInt(nextLine[i]);
            }
            int i = 0;
            while ((nextLine = reader.readNext()) != null) {
                for (int j = 0; j < phges; j++) {
                    kostos[i][j] = Integer.parseInt(nextLine[j]);
                }
                i++;
            }
            input = new Input(phges, sygkentrotes, fortio, xwritikothta, kostos);
        } catch (IOException ex) {
            Logger.getLogger(DmcnApplicationMainUi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
